const fs = require('fs')
//process.env.UNI_INPUT_DIR为项目所在的绝对路径，经测试，相对路径会找不到文件
const manifestPath = process.env.UNI_INPUT_DIR + '/manifest.json'
let Manifest = fs.readFileSync(manifestPath, {
	encoding: 'utf-8'
})

function replaceManifest(path, value) {
	const arr = path.split('.')
	const len = arr.length
	const lastItem = arr[len - 1]

	let i = 0
	let ManifestArr = Manifest.split(/\n/)

	for (let index = 0; index < ManifestArr.length; index++) {
		const item = ManifestArr[index]
		if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
		if (i === len) {
			const hasComma = /,/.test(item)
			ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`),
				`"${lastItem}": ${value}${hasComma ? ',' : ''}`)
			break;
		}
	}

	Manifest = ManifestArr.join('\n')
}
//动态配置 appid和名称 ,这部分的代码可根据自己的需要写，需要改什么，按照manifest.json对应的路径写好即可
replaceManifest('mp-weixin.appid', `"${process.UNI_SCRIPT_ENV.UNI_BASE_APPID}"`)
replaceManifest('name', `"${process.UNI_SCRIPT_ENV.UNI_BASE_NAME}"`)

// console.log(process)
fs.writeFileSync(manifestPath, Manifest, {
	"flag": "w"
})